-------------------------------------------------------------------------------
---! CREATE TABLE TO HOLD RANK
-------------------------------------------------------------------------------
local user_cache_map = {}
local user_rank_num = 200
local user_rank_map = nil

-------------------------------------------------------------------------------
---! 内部方法
-------------------------------------------------------------------------------
---! 加载排行榜数据
local _last_load_time
local function load_rank_data()
    if user_rank_map then
        ---! 数据库数据仅需加载成功一次即可
        return true
    end

    local now = os.time()
    if _last_load_time and now - _last_load_time < 30 then
        return false
    end

    _last_load_time = now
    local ok, data = FILE_D:read_common_data("stage_rank", user_rank_num)
    _last_load_time = nil

    if not ok then
        ---! 查询数据库失败
        return false
    end

    user_rank_map = data
    return true
end

---! 查询玩家数据
local function find_user_data(userid, user_rank_tmp)
    userid = tostring(userid)
    for idx = 1, #user_rank_tmp do
        local rank = user_rank_tmp[idx]
        if rank.userid == userid then
            return idx, rank
        end
    end
end

---! 同步排行榜缓存
local function sync_cache_data()
    local ok = load_rank_data()
    if not ok then
        return false
    end

    if table.size(user_cache_map) <= 0 then
        return true
    end

    ---! 每次同步使用全新数据，避免影响玩家个人缓存数据
    user_rank_map = table.copy(user_rank_map)

    ---! 同步一次玩家数据
    for userid, user_data in pairs(user_cache_map) do
        local _, rank = find_user_data(userid, user_rank_map)
        if not rank then
            table.insert(user_rank_map, user_data)
        else
            rank.level = user_data.level
            rank.stage_level = user_data.stage_level
            rank.nickname = user_data.nickname
            rank.context = user_data.context
        end
    end

    ---! 进行一次数据排序
    table.sort(user_rank_map, function (a, b)
        return a.stage_level * 10000 + a.level > b.stage_level * 10000 + b.level
    end)

    ---! 清理多余排名数据
    for idx = user_rank_num + 1, #user_rank_map do
        user_rank_map[idx] = nil
    end

    ---! 重置玩家缓存数据
    user_cache_map = {}
    return true
end

-------------------------------------------------------------------------------
---! 对外接口
-------------------------------------------------------------------------------
RANK_D = {}

---! 查询排行榜数据
function RANK_D:query_rank_data(player, name, from, to)
    if name == "stage_rank" then
        return STAGE_RANK_D:query_rank_data(player, from, to)
    end

    if name == "wings_rank" then
        return WINGS_RANK_D:query_rank_data(player, from, to)
    end

    if name == "skills_rank" then
        return SKILLS_RANK_D:query_rank_data(player, from, to)
    end

    if name == "partner_rank" then
        return PARNTER_RANK_D:query_rank_data(player, from, to)
    end

    if name == "equip_rank" then
        return EQUIP_RANK_D:query_rank_data(player, from, to)
    end

    if name == "artifact_rank" then
        return ARTIFACT_RANK_D:query_rank_data(player, from, to)
    end
end

---! 同步排行榜数据
function RANK_D:sync_rank_data(player, name)
    if not name then
        STAGE_RANK_D:sync_rank_data(player)
        WINGS_RANK_D:sync_rank_data(player)
        SKILLS_RANK_D:sync_rank_data(player)
        PARNTER_RANK_D:sync_rank_data(player)
        EQUIP_RANK_D:sync_rank_data(player)
        ARTIFACT_RANK_D:sync_rank_data(player)
        return
    end

    if name == "stage_rank" then
        return STAGE_RANK_D:sync_rank_data(player)
    end

    if name == "wings_rank" then
        return WINGS_RANK_D:sync_rank_data(player)
    end

    if name == "skills_rank" then
        return SKILLS_RANK_D:sync_rank_data(player)
    end

    if name == "partner_rank" then
        return PARNTER_RANK_D:sync_rank_data(player)
    end

    if name == "equip_rank" then
        return EQUIP_RANK_D:sync_rank_data(player)
    end

    if name == "artifact_rank" then
        return ARTIFACT_RANK_D:sync_rank_data(player)
    end
end
